package bigo.multiplication;

import org.apache.hadoop.conf.Configuration;

/**
 * The partition schema that stores how the matrices are partitioned.
 * reviewed @ Dec 4
 * @author Song Liu (sl9885@bristol.ac.uk)
 */
public class PartitionSchema {

    public int numRowBlocksA, numColBlocksA, numRowBlocksB, numColBlocksB;

    public PartitionSchema(int numRowBlocksA, int numbColBlocksA, int numRowBlocksB, int numColBlocksB) {
        this.numRowBlocksA = numRowBlocksA;
        this.numColBlocksA = numbColBlocksA;
        this.numRowBlocksB = numRowBlocksB;
        this.numColBlocksB = numColBlocksB;
    }

    public PartitionSchema() {
    }

    /**
     * this method allows this schema can be restored from the configuration
     * string
     * @param initString
     */
    public PartitionSchema(String initString) {
        numRowBlocksA = Integer.parseInt(initString.split("\t")[0]);
        numColBlocksA = Integer.parseInt(initString.split("\t")[1]);
        numColBlocksB = Integer.parseInt(initString.split("\t")[2]);
        numRowBlocksB = Integer.parseInt(initString.split("\t")[3]);
    }

    /**
     * construct a new partition schema from the configuration string
     * @param conf
     * @return
     */
    public static PartitionSchema loadFromConfiguration(Configuration conf) {
        return new PartitionSchema(conf.get("sch"));
    }

    /**
     * store current partition schema to the configuration
     * @param conf
     */
    public void writeToConfiguration(Configuration conf) {
        conf.set("sch", this.toString());
    }

    @Override
    public String toString() {
        return numRowBlocksA + "\t" + numColBlocksA + "\t" + numColBlocksB + "\t" + numRowBlocksB;
    }
}
